<?php

namespace App\Http\Controllers\OA;

use App\Http\Controllers\Controller;
use App\Http\Requests\NoticeRequest;
use App\Http\Resources\NoticeResource;
use App\Model\OAGongGaos;
use Illuminate\Http\Request;

class NoticesController extends Controller
{
    public function index(Request $request)
    {
        $list = OAGongGaos::query()
            ->when($request->filled('title'),function ($q) use ($request){
                return $q->where('title','like',$request->title . '%');
            })
            ->paginate($request->limit ?? 10);

        return $this->SucRes(NoticeResource::collection($list));
    }

    public function store(NoticeRequest $request)
    {
        $params = $this->mergeRequest($request);
        $params['publish_at'] = now()->toDateTimeString();
        $notice = OAGongGaos::query()->create($params);
        return $this->SucRes(['id'=>$notice->id]);
    }

    public function update(NoticeRequest $request,OAGongGaos $notice)
    {
        $params = $this->mergeRequest($request);
        $params['publish_at'] = now()->toDateTimeString();
        $notice->fill($params)->save();
        return $this->SucRes();
    }

    public function destroy(OAGongGaos $notice)
    {
        $notice->delete();
        return $this->SucRes();
    }
}
